package userHelp

import (
	myConfig "common/config"
	myHelper "common/helper"
	myredis "common/redis"
	"fmt"
	"time"
)

/*
储蓄罐功能
*/
var InterestPeriod int64 = 60 * 60

type TblPiggyBank struct {
	Uid              string  `json:"uid" cbor:"uid" db:"uid"`
	PiggyAmount      float64 `json:"amount" cbor:"amount" db:"amount"`                                     //储蓄罐金额
	Interest         float64 `json:"interest" cbor:"interest" db:"interest"`                               //当前利息
	TotalInterest    float64 `json:"total_interest" cbor:"total_interest" db:"total_interest"`             //已领取的总利息
	InterestCalcTime int64   `json:"interest_calc_time" cbor:"interest_calc_time" db:"interest_calc_time"` //利息开始计算的时间
	InterestEndTime  int64   `json:"interest_end_time" cbor:"interest_end_time" db:"interest_end_time"`    //本次利息结束的时间
}

var g_tbl_member_piggy_bank string = "tbl_member_piggy_bank:"

// 增加储蓄罐金额 PiggyAmount > 0 则充值  PiggyAmount <0  提现到钱包
func AddPiggyAmount(uid string, PiggyAmount float64) {
	LoadUserToRedis(uid)
	myredis.AddUserFieldValueByFloat64(uid, g_tbl_member_piggy_bank+"amount", PiggyAmount)
	ResetInterestCalcTime(uid)
	ResetInterestEndTime(uid)
}

func GetPiggyAmount(uid string) float64 {
	LoadUserToRedis(uid)
	return myredis.GetUserFieldFloat64(uid, g_tbl_member_piggy_bank+"amount")
}

func AddInterest(uid string, Interest float64) {
	LoadUserToRedis(uid)
	myredis.AddUserFieldValueByFloat64(uid, g_tbl_member_piggy_bank+"interest", Interest)
	myHelper.InfoLog("AddInterest:%s Interest:%f", uid, Interest)
}

func GetInterest(uid string) float64 {
	LoadUserToRedis(uid)
	return myredis.GetUserFieldFloat64(uid, g_tbl_member_piggy_bank+"interest")
}

func AddTotalInterest(uid string, TotalInterest float64) {
	LoadUserToRedis(uid)
	myredis.AddUserFieldValueByFloat64(uid, g_tbl_member_piggy_bank+"total_interest", TotalInterest)
}

func GetTotalInterest(uid string) float64 {
	LoadUserToRedis(uid)
	return myredis.GetUserFieldFloat64(uid, g_tbl_member_piggy_bank+"total_interest")
}

func GetInterestCalcTime(uid string) int64 {
	LoadUserToRedis(uid)
	return myredis.GetUserFieldInt64(uid, g_tbl_member_piggy_bank+"interest_calc_time")
}

func ResetInterestCalcTime(uid string) {
	LoadUserToRedis(uid)
	TimeNow := fmt.Sprintf("%d", int(time.Now().Unix()))
	myredis.SetUserFieldValue(uid, g_tbl_member_piggy_bank+"interest_calc_time", TimeNow)
}

func GetInterestEndTime(uid string) int64 {
	LoadUserToRedis(uid)
	return myredis.GetUserFieldInt64(uid, g_tbl_member_piggy_bank+"interest_end_time")
}

func ResetInterestEndTime(uid string) {
	LoadUserToRedis(uid)
	TimeNow := fmt.Sprintf("%d", int(time.Now().Unix()))
	myredis.SetUserFieldValue(uid, g_tbl_member_piggy_bank+"interest_end_time", TimeNow)
}

func CalcInterest(uid string) float64 {
	amount := GetPiggyAmount(uid)
	return amount * myConfig.GetGameCfgFloat64(myConfig.CfgPiggyBankReturn) / (365 * 24)
}

/*
判断是否可以领取利息  当天只能领取昨天的利息
*/
func CanAchieveInterest(uid string) bool {
	CalcEndTime := GetInterestEndTime(uid)
	timeNow := time.Now().Unix()

	if myHelper.InSameDay(CalcEndTime, timeNow) {
		return false
	}

	if GetInterest(uid) == 0.00 {
		return false
	}

	return true
}

/*
生成利息
*/
func GenInterest(uid string) {

	//有前期未领取的利息 不继续产生利息
	if GetInterest(uid) > 0.00 && CanAchieveInterest(uid) {
		return
	}

	CalcBeginTime := GetInterestCalcTime(uid)
	CalcEndTime := GetInterestEndTime(uid)

	//此笔储蓄罐已经产生利息
	if CalcEndTime-CalcBeginTime >= InterestPeriod {
		return
	}

	timeNow := time.Now().Unix()
	Interest := CalcInterest(uid)
	if myHelper.InSameDay(CalcBeginTime, timeNow) {
		if timeNow-CalcBeginTime >= InterestPeriod && CalcEndTime-CalcBeginTime < InterestPeriod {
			AddInterest(uid, Interest)
			ResetInterestEndTime(uid)
		}
	} else {
		//隔天
		if time.Now().Hour() >= 1 {
			AddInterest(uid, Interest)
			ResetInterestCalcTime(uid)
			ResetInterestEndTime(uid)
		}
	}
}
